
package pl.genapp.stocksense;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import pl.genapp.appsense.R;
import pl.genapp.stocksense.libraries.HttpMakerAsyncTask;
import pl.genapp.stocksense.libraries.JSONStringMaker;
import pl.genapp.stocksense.others.PostExecuteReaction;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class NewsScreenFragment extends Fragment {
	
	/**
	 * Important ;)
	 */
	static final String[]	NEWS_TYPES		= { "CALM", "ALERT", "SURE", "VITAL", "KIND", "HAPPY" };
	static final int		NEWS_PER_PAGE	= 5;
	
	// ktory rodzaj newsow wyswietlamy
	// Typ czyli ktore newsy... calm? alert? sure?....
	int						type;
	
	ListView				newsListView;
	NewsAdapter				newsAdapter;
	
	int						pageNumber		= 0;
	
	class NewsContainer {
		public String	title;
		public String	url;
		public String	content;
		public String	date;
	}
	
	List<NewsContainer>				news				= new ArrayList<NewsContainer>();
	
	PostExecuteReaction<JSONObject>	onGetNewsReaction	= new PostExecuteReaction<JSONObject>() {
															
															public void onSuccess(Context context, JSONObject object) {
																
																try {
																	JSONArray dataArray = object.getJSONArray("data");
																	
																	for (int i = 0; i < dataArray.length(); i++) {
																		
																		JSONObject newsData = dataArray.getJSONObject(i);
																		
																		NewsContainer oneNews = new NewsContainer();
																		
																		oneNews.title = newsData.getString("title");
																		oneNews.url = newsData.getString("url");
																		oneNews.content = newsData.getString("content");
																		oneNews.date = newsData.getString("date");
																		
																		news.add(oneNews);
																		Log.d("stocksense", oneNews.title);
																		
																	}
																} catch (Exception e) {
																	Log.d("stocksense", "exception Newsy!" + e.toString());
																}
																
																newsAdapter.notifyDataSetChanged();
																Log.d("stocksense", "Newsy!");
																
															}
															
														};
	
	public static NewsScreenFragment newInstance(int type) {
		
		NewsScreenFragment fragment = new NewsScreenFragment();
		
		Bundle arg = new Bundle();
		arg.putInt("type", type);
		
		fragment.setArguments(arg);
		
		return fragment;
		
	}
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// ustawiamy typ
		type = getArguments().getInt("type");
	}
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		View layout = inflater.inflate(R.layout.fragment_news_screen, container, false);
		
		((TextView) layout.findViewById(R.id.newsType)).setText(NEWS_TYPES[type - 1]);
		
		newsAdapter = new NewsAdapter();
		
		// footer
		View footer = getActivity().getLayoutInflater().inflate(R.layout.news_more_button, null);
		footer.findViewById(R.id.loadMoreButton).setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				loadNews();
				Log.d("stocksense", "klikam wiecej!");
			}
		});
		
		// listview
		newsListView = (ListView) layout.findViewById(R.id.newsListView);
		newsListView.addFooterView(footer);
		newsListView.setAdapter(newsAdapter);
		
		newsListView.setOnItemClickListener(new ListView.OnItemClickListener() {
			
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
				
				// create fragment
				NewsShowScreenFragment newsShowFragment = NewsShowScreenFragment.newInstance(news.get(position).url);
				
				// fragment manager
				FragmentManager fragmentManager = getFragmentManager();
				FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
				
				//
				fragmentTransaction.replace(R.id.fragmentsSide, newsShowFragment).addToBackStack(null);
				fragmentTransaction.commit();
				
			}
			
		});
		
		// ladujemy newsy
		loadNews();
		
		return layout;
		
	}
	
	void loadNews() {
		String params = JSONStringMaker.getNews(getActivity(), NEWS_PER_PAGE, pageNumber++ * NEWS_PER_PAGE);
		new HttpMakerAsyncTask(getActivity(), onGetNewsReaction).execute(params);
	}
	
	/**
	 * Adpater
	 */
	
	class NewsAdapter extends BaseAdapter {
		
		public int getCount() {
			return news.size();
		}
		
		public Object getItem(int arg0) {
			return arg0;
		}
		
		public long getItemId(int arg0) {
			return arg0;
		}
		
		public View getView(int position, View view, ViewGroup arg2) {
			
			TextView title;
			TextView url;
			TextView content;
			
			if (view == null) {
				view = getActivity().getLayoutInflater().inflate(R.layout.news_list_item, null);
			}
			
			title = (TextView) view.findViewById(R.id.news_title);
			url = (TextView) view.findViewById(R.id.news_url);
			content = (TextView) view.findViewById(R.id.news_content);
			
			title.setText(news.get(position).title);
			url.setText(news.get(position).url);
			content.setText(news.get(position).content);
			
			return view;
			
		}
		
	}
	
}
